/**
 * @Author: wangliuyang
 * @Date: 2021/9/6 15:45
 * @Description:
 */
public class Solution_43 {
    public String multiply(String num1, String num2) {
        if(num1.startsWith("0") || num2.startsWith("0")){
            return "0";
        }
        int pre = 0;
        int index1 = num1.length() - 1;
        int index2 = num2.length() - 1;
        StringBuilder stringBuilder = new StringBuilder();
        while (index1 >=0 && index2 >= 0){
            int result = Integer.parseInt(String.valueOf(num1.charAt(index1))) * Integer.parseInt(String.valueOf(num2.charAt(index1))) + pre;
            pre = result / 10;
            stringBuilder.append(result % 10);
            index1--;
            index2--;
        }
        if (index2 == -1 && index1 == -1){
            if (pre == 0){
                return stringBuilder.reverse().toString();
            }else {
                return stringBuilder.append(pre).reverse().toString();
            }

        }
        if (pre == 0){
            if (index1 == -1){
                while (index2 != -1){
                    stringBuilder.append(num2.charAt(index2--));
                }
            }else {
                while (index1 != -1){
                    stringBuilder.append(num2.charAt(index1--));
                }
            }
            return stringBuilder.reverse().toString();
        }
        String multiply = "";
        if (index1 == -1){
            multiply  = multiply(num2.substring(0, index2 + 1), String.valueOf(pre));
        }else {
             multiply = multiply(num2.substring(0,index1 + 1),String.valueOf(pre));
        }
        return multiply + stringBuilder.reverse().toString();
    }
}
